import { Node, log, Sprite, SpriteFrame, sys } from "cc"
import { ccclass, EUILayer, EBlockOnceAdType, _scene, _platform, EPlatformType, EBottomAdType, _ui, _rank, _logic, _gameType, property } from "../Main"
import { AnimBaseUI } from "./AnimBaseUI"
import { EGameType } from "../module/define/GameTypeDefine"
import { losTools } from "db://assets/app/Script/Tools/losTools"
import { StaminaSystem } from "db://assets/app/Script/StaminaSystem/StaminaSystem"
import { losChannel } from "db://assets/app/Script/SDK/losChannel"
import loskc from "db://assets/app/Script/SDK/loskc"


@ccclass("IndexUI")
export class IndexUI extends AnimBaseUI {
	@property({ type: Sprite })
	public logo: Sprite = null;

	@property({ type: [SpriteFrame] })
	//0:合体吧块块 1:对号入球 2:球球去哪儿 3:球球传说
	public logoSpriteFrame: SpriteFrame[] = [];

	public layerType = EUILayer.WindowUp
	public blockOnceAdType = EBlockOnceAdType.Show
	public bottomAdType = EBottomAdType.None

	private _loginNode: Node = null;

	private cPower: Node = null!
	private cPowerItem: Node = null!
	private cDesktop: Node = null!
	private cFavorite: Node = null!
	protected onPreLoad(): void {

	}
	private onClickADefault() {

		this.clickRun(EGameType.endless)
	}

	private onClickLevel() {

		this.clickRun(EGameType.level)
	}
	private onClickPower() {
		losTools.showInfiniteStaminaUI()
	}
	private onClickDesktop() {
		losChannel.instance().addShortcut({
			sucCall: () => {
				this.cDesktop.active = false;
			}, failCall: () => {
				// this.desktopNode.active = false;
			}
		})
	}
	private onClickFavorite() {
		losChannel.instance().addCommonUse({
			sucCall: () => {
				loskc.log("onCommUse - 添加常用成功")
				this.cFavorite.active = false
			},
			failCall: () => {
				loskc.log("onCommUse - 添加常用失败")
			}
		})
	}

	private clickRun(type: EGameType) {
		const channelName = losChannel.instance().channel
		if (channelName == 'ks'
			|| channelName == 'ks2'
			|| channelName == 'ks3'
			|| channelName == 'hw'
			|| channelName == 'honor') {
			if (StaminaSystem.instance.getStamina() < 5) {
				_ui.tip('体力不足')
				return
			}
			StaminaSystem.instance.reduceStamina(5)
		}
		if (_gameType.hasRun(type))
			this.playAnim(false, () => {
				if (!_gameType)
					return
				if (!this.scene) {
					_gameType.type = type
					_ui.Loading.wait(true)
					return
				}

				_gameType.run(type)
			})
		switch (type) {
			case EGameType.endless:
				{

				}
				break
			case EGameType.level:
				{

				}
				break
		}
		losChannel.instance().doAction('进入游戏弹三个叠加为一组的视频', ['hw']);
	}
	private isFirstEnter = true;
	onEnable(): void {
		log("IndexUI onEnable")
		this.cPower.active = false;
		this.cPowerItem.active = false;

		const channelName = losChannel.instance().channel
		switch (channelName) {
			case 'vivo':
			case 'oppo':
			case 'xiaomi':
				this.logo.spriteFrame = this.logoSpriteFrame[0];
				break;
			case 'oppo1':
			case 'vivo1':
			case 'mg':
				{
					this.logo.spriteFrame = this.logoSpriteFrame[1];
				}
				break;
			case 'hw':
			case 'ks':
			case 'honor':
				{
					this.logo.spriteFrame = this.logoSpriteFrame[1];
					this.cPower.active = true;
					this.cPowerItem.active = true;
				}
				break;
			case 'ks2':
				{
					this.logo.spriteFrame = this.logoSpriteFrame[2];
					this.cPower.active = true;
					this.cPowerItem.active = true;
				}
				break;
			case 'ks3':
				{
					this.logo.spriteFrame = this.logoSpriteFrame[3];
					this.cPower.active = true;
					this.cPowerItem.active = true;
				}
				break
			// default:
			// 	this.logo.spriteFrame = this.logoSpriteFrame[1];
			// 	this.cPower.active = true;
			// 	this.cPowerItem.active = true;
			// 	break;
		}
		if (losChannel.instance().canSupportAddShortcut()) {
			losChannel.instance().checkShortcut({
				sucCall: () => {
					loskc.log("需要添加桌面")
					this.cDesktop.active = true;
				}, failCall: () => {
					loskc.log("已添加桌面")
					this.cDesktop.active = false;
				}
			})
		}
		if (losChannel.instance().canSupportCommonUse()) {
			//检测添加常用
			losChannel.instance().checkCommonUse({
				sucCall: () => {
					loskc.log("需要添加常用")
					this.cFavorite.active = true
				},
				failCall: () => {
					loskc.log("已添加常用")
					this.cFavorite.active = false
				}
			})
		}

		const loginTime = sys.localStorage.getItem('loginGetRewardTime', Date.now().toString()) || 0

		// if (losTools.isNewDay(parseInt(loginTime))) {
		// 	if (!this._loginNode)
		// 		losTools.showLoginReward()
		// }
		if (this.isFirstEnter) {
			this.isFirstEnter = false;

		}
		losChannel.instance().doAction('常驻banner', ['hw']);
		losChannel.instance().doAction('loading结束强弹视频广告', ['hw']);

	}
}
